import time
import serial
import string
import ctypes
import os

#convert string to hex
dc
def CalculateCRC(databytes):
    preset_value = 0XFFFF
    polynomial = 0X8408
    crcvalue = preset_value
    databytes = databytes[2:]
    for i in databytes:
         crcvalue = crcvalue ^ i
         for j in range(7):
              if (crcvalue & 0x01 == 1):
                   crcvalue = (crcvalue >> 1) ^ polynomial
              else:
                   crcvalue >>= 1
    crcvalue = ~ crcvalue - 65536
    crclsb = crcvalue % 256
    crcmsb = (crcvalue - crclsb) / 256
         

# load dll
from ctypes import *
#cdll.LoadLibrary("libc.so.6")
#libc = CDLL("libc.so.6")
#libc
#apsx = CDLL('apsxdll.dll')
#apsx_lib_filepath = os.path.join(os.path.dirname(__file__), 'APSXDLL.dll')
#apsx_lib = cdll.LoadLibrary(apsx_lib_filepath)

#apsx = windll.LoadLibrary("apsxdll.dll")
#apsx.GetUID()

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(0, 19200, timeout=1)

ser.open()
ser.isOpen()
print ser.portstr
ser.flushInput()
ser.flushOutput()
out = ''
ser.write(chr(0xFD)) #toggle red light on
time.sleep(2) #wait 2 seconds
ser.write(chr(0xFC)) #toggle red light off
time.sleep(2) #wait 2 seconds
#ser.write(chr(0xFE)) #toggle green light off
#time.sleep(2) #wait 2 seconds
#ser.write(chr(0xFF)) #toggle green light on
#time.sleep(2) #wait 2 seconds
#ser.write(chr(0xFB)) #read tag (ReadUID)
#time.sleep(2) #wait 2 seconds
#Transmitter OFF
#ser.write(chr(0x01)+chr(0x09)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x49)+chr(0x00)+chr(0x46)+chr(0xB9))
#time.sleep(2)
#Transmitter ON
#ser.write(chr(0x01)+chr(0x08)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x48)+chr(0x46)+chr(0xB9))
#time.sleep(2)
#quiet tag B: 00 00 f1 6f 8a 12 00 01 04 e0 fb 48
#ser.write(chr(0x01)+chr(0x10)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x64)+chr(0xF1)+chr(0x6F)+chr(0x8A)+chr(0x12)+chr(0x00)+chr(0x01)+chr(0x04)+chr(0xE0)+chr(0x23)+chr(0xDC))
#time.sleep(2) #wait 2 seconds
#ser.write(chr(0x06)+chr(0x0C)+chr(0x27)+chr(0x01)+chr(0x00)+chr(0x2A)+chr(0x50)) #GetUID
#ser.write(chr(0xFA)) #read tag (ReadUID)
#time.sleep(2) #wait 2 seconds
#out = ser.readline()
#print toHex(out)

#Pass-thru read single block
ser.write(chr(0x01)+chr(0x0D)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x45)+chr(0x03)+chr(0x20)+chr(0x01)+chr(0x12)+chr(0x1B)+chr(0x65)+chr(0x9A))
time.sleep(0.5)
#ser.write(chr(0xFA)) #read tag (Fast Mode)
#time.sleep(2) #wait 2 seconds
out = ser.readline()
print toHex(out)

# Select Request (6A)
#ser.write(chr(0x01)+chr(0x10)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x6A)+ chr(0x00)+chr(0x00)+chr(0xF1)+chr(0x6F)+chr(0x8A)+chr(0x12)+chr(0x00)+chr(0x01)+chr(0x04)+chr(0xE0)+chr(0xFB)+chr(0x48)+chr(0x49)+chr(0xB6))
#time.sleep(2)
#Select tag B: 00 00 f1 6f 8a 12 00 01 04 e0 fb 48
#ser.write(chr(0x01)+chr(0x10)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x6A)+chr(0xF1)+chr(0x6F)+chr(0x8A)+chr(0x12)+chr(0x00)+chr(0x01)+chr(0x04)+chr(0xE0)+chr(0x49)+chr(0xB6))
#time.sleep(1)
# Reset to Ready
#ser.write(chr(0x01)+chr(0x09)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x6B)+ chr(0x01)+chr(0x48)+chr(0xB7))
#time.sleep(2) #wait 2 seconds
#ser.write(chr(0x06)+chr(0x0C)+chr(0x27)+chr(0x01)+chr(0x00)+chr(0x2A)+chr(0x50)) #GetUID
#ser.write(chr(0xFA)) #read tag (Fast Mode)
#time.sleep(2) #wait 2 seconds
#out = ser.readline()
#print toHex(out)

#quiet tag H: 00 00 09 d8 8a 12 00 01 04 e0 93 b1
#ser.write(chr(0x01)+chr(0x10)+chr(0x00)+chr(0x03)+chr(0x04)+chr(0x64)+chr(0x09)+chr(0xD8)+chr(0x8A)+chr(0x12)+chr(0x00)+chr(0x01)+chr(0x04)+chr(0xE0)+chr(0x23)+chr(0xDC))
#time.sleep(2) #wait 2 seconds
#ser.write(chr(0x06)+chr(0x0C)+chr(0x27)+chr(0x01)+chr(0x00)+chr(0x2A)+chr(0x50)) #GetUID
ser.write(chr(0xFA)) #read tag (ReadUID)
time.sleep(2) #wait 2 seconds
out = ser.readline()
print toHex(out)

# Find Token (pg.8)
#ser.write(chr(0x01) + chr(0x09) + chr(0x00) + chr(0x03) + chr(0x04) + chr(0x41) + chr(0x01) + chr(0x44) + chr(0xBB))
#time.sleep(2) #wait 2 seconds
#ser.write(chr(0x06) + chr(0x14) + chr(0x03) + chr(0x20) + chr(0x00) + chr(0x9b) + chr(0x0A))
#ser.write(chr(0x01) +chr(0x00)+ chr(0x02)+chr(0xFF) + chr(0xCA) + chr(0x00) + chr(0x00) + chr(0x00))
#time.sleep(2) #wait 2 seconds
#out = ser.readline()
#print toHex(out)

ser.close()
